*replace V:\docs ... below with location .dta file
local path V:\docs\sc_ops\Lempert_SOP_JLC_rep
use "`path'\sop_data_compact.dta"



*this var is useful later.
gen tmcli=__cli if __aumaj==1
egen __mcli=mode(tmcli), by(caseid)
drop tmcli
la var __mcli "Majority CLI for case"

 

 
*recreate oww constrained vars.  
 
egen tvmm=median(JCS20_jip) if majority==2, by(caseid)
egen __jmajmed=mode(tvm), by (caseid) 
la var __jmajmed "Median of Majority Coalition IP, DW-N D1"
drop tvmm

egen __jcasemed=median(JCS20_jip) if majority !=., by(caseid)
la var __jcasemed "Ct Median (Case Participants only), DW-N D1"


gen __oww_dfp=min(abs(__jmajmed-__leftfp),abs(__jmaj-__rightfp),abs(__jmaj-__hmed))
replace __oww_dfp=0 if __jmajmed > min(__leftfp,__rightfp,__hmed) & ///
__jmajmed < max(__leftfp,__rightfp,__hmed) 
la var __oww_dfp "Dist to Fil Pivot (OWW method)"
 
 
gen __oww_dfm=min(abs(__jmajmed-__senmed),abs(__jmajmed-__hmed))
replace __oww_dfm=0 if __jmajmed > min(__hmed, __senmed) & ///
__jmajmed < max(__hmed, __senmed)
la var __oww_dfm "Dist to Floor Median (OWW method)"

gen __oww_djcm=min(abs(__jmajmed-__sjcmed),abs(__jmajmed-__hjcmed))
replace __oww_djcm=0 if __jmajmed > min(__hjcmed, __sjcmed) & ///
__jmajmed < max(__hjcmed, __sjcmed)
la var __oww_djcm "Dist to Jud. Comm. Median (OWW method)"

gen __oww_dmm=min(abs(__jmajmed-__majsenmed),abs(__jmajmed-__majhmed))
replace __oww_dmm=0 if __jmajmed > min(__majhmed, __majsenmed) & ///
__jmajmed < max(__majhmed, __majsenmed)
la var __oww_dmm "Dist to Majority Party Median (OWW method)"


***controls 

*construct amicus var, based on Collins and Dino's data:
gen __abct=AC_totalac
replace __abct=__briefct if AC_totalac==.
la var __abct "Amicus breifs/caseid (Collins, Dino)"
replace __abct=0 if __abct==. & term > 2000 & term < 2013 & term!=.  

*granted to resolve conflict
gen __gconflict=.
recode __gconflict(.=1) if certreason==2 | certreason==4 | certreason==5 | certreason==6
recode __gconflict(.=0) if certreason==1 | certreason==3 | certreason==7 | certreason==8 | certreason==9 ///
| certreason==10 | certreason==11 | certreason==12 | certreason==13 
label var __gconflict "Gr Reason: Conflict Resolve"



*alteration of precedent is already in as binary var: "precedentalteration"

*struck down federal law:

gen __ujudrev=declarationu-1
recode __ujudrev (2=0) (3=0)
tab __ujudrev declarationu //see this works

*coalition heterogeneity
egen tvmqsd=sd(post_mn) if majority ==2 ,by(caseid)
egen __mqsd=mode(tvmqsd), by(caseid)
label var __mqsd "Standard Deviation of Majority Coalition Ideology"
drop tvmqsd

*dissent coalition heterogeneity
egen tvdmq=sd(post_mn) if majority==1, by(caseid)
replace tvdmq=0 if tvdmq==. & majority==1 & post_mn !=.  //b/c single obs's sd calculated as ., not 0.
egen __dmqsd=mode(tvdmq), by(caseid)
la var __dmqsd "Standard Deviation of Dissent Coalition Ideology"
drop tvdmq

gen __cmqsd=__mqsd if __aumaj==1
replace __cmqsd=__dmqsd if __aumaj !=1 &  __cli !=.
la var __cmqsd "__mqsd if obs is maj op; __dmqsd if obs is dissent op"



*recode a single obs (case) coded as "private action" to "misc" for issue area, 
*not entirely kosher w/o renaming var, but oh well:

recode issuearea(14=13) if term <=2012 & term >=1947



*indicator that, for an unconstrained court, the dissent is in the same direction away from
*majority as the closest pivot.--> =~ that dissent is more compatible w/ Congress 
*than majority.  This should be true in most cases.  Note that this var is not used 
*in this version of .do file; rather the more precise addir... vars are used--constructed below.

egen tvdm=median(JCS20_jip) if majority==1, by(caseid)
egen __jdismed=mode(tvdm), by (caseid) 
la var __jdismed "Median of Dissenting Coalition IP, DW-N D1"
drop tvdm

*we violate naming convention here to allow use of the var variants in a loop
gen ddir__oww_dfp=0 if __jdismed !=.
replace ddir__oww_dfp=1 if __oww_dfp==0 & __jdismed !=.
replace ddir__oww_dfp=1 if __jmajmed < min(__leftfp,__rightfp,__hmed) & __jdismed > __jmajmed & __jdismed !=.
replace ddir__oww_dfp=1 if __jmajmed > max(__leftfp,__rightfp,__hmed) & __jdismed < __jmajmed & __jdismed !=.

la var ddir__oww_dfp "1: If maj const & dis is in dir of cong (FP), rel to maj; or maj unconst"
*this var is to define a sample


gen ddir__oww_dfm=0 if __jdismed !=.
replace ddir__oww_dfm=1 if __oww_dfm==0 & __jdismed !=.
replace ddir__oww_dfm=1 if __jmajmed < min(__senmed,__hmed) & __jdismed > __jmajmed & __jdismed !=.
replace ddir__oww_dfm=1 if __jmajmed > max(__senmed,__hmed) & __jdismed < __jmajmed & __jdismed !=.
 
la var ddir__oww_dfm "1: If maj const & dis is in dir of cong (Floor Med), rel to maj; or maj unconst"


gen ddir__oww_djcm=0 if __jdismed !=.
replace ddir__oww_djcm=1 if __oww_djcm==0 & __jdismed !=.
replace ddir__oww_djcm=1 if __jmajmed < min(__sjcmed,__hjcmed) & __jdismed > __jmajmed & __jdismed !=.
replace ddir__oww_djcm=1 if __jmajmed > max(__sjcmed,__hjcmed) & __jdismed < __jmajmed & __jdismed !=.
 
la var ddir__oww_djcm "1: If maj const & dis is in dir of cong (JC Med), rel to maj; or maj unconst"

 
gen ddir__oww_dmm=0 if __jdismed !=.
replace ddir__oww_dmm=1 if __oww_dmm==0 & __jdismed !=.
replace ddir__oww_dmm=1 if __jmajmed < min(__majsenmed,__majhmed) & __jdismed > __jmajmed & __jdismed !=.
replace ddir__oww_dmm=1 if __jmajmed > max(__majsenmed,__majhmed) & __jdismed < __jmajmed & __jdismed !=.
 
la var ddir__oww_dmm "1: If maj const & dis is in dir of cong (Maj Med), rel to maj; or maj unconst"



/*alternative version of sample variable, excluding cases where dissent is not closer
to (midpoint of house and senate) policy output than to majority
i.e., drop if |J_maj-J_diss| < |J_diss-.5(H_med/majmed+S_med/majmed)|
*/


*la var __jdismed "Median of Dissenting Coalition IP, DW-N D1"

*we violate naming convention here to allow use of the var variants in a loop
gen addir__oww_dfp=0 if __jdismed !=.
replace addir__oww_dfp=1 if __oww_dfp==0 & __jdismed !=.
replace addir__oww_dfp=1 if abs(__jmajmed-__jdismed) > abs(__jdismed-.5*(__hmed+__senmed)) & __jdismed !=.


la var addir__oww_dfp "1: If dis closer to (mp of house & sen) policy output than maj"
*this var is to define a sample


gen addir__oww_dfm=0 if __jdismed !=.
replace addir__oww_dfm=1 if __oww_dfm==0 & __jdismed !=.
replace addir__oww_dfm=1 if abs(__jmajmed-__jdismed) > abs(__jdismed-.5*(__hmed+__senmed)) & __jdismed !=.


la var addir__oww_dfm "1: If dis closer to (mp of house & sen) policy output than maj"


gen addir__oww_djcm=0 if __jdismed !=.
replace addir__oww_djcm=1 if __oww_djcm==0 & __jdismed !=.
replace addir__oww_djcm=1 if abs(__jmajmed-__jdismed) > abs(__jdismed-.5*(__hmed+__senmed)) & __jdismed !=.

la var addir__oww_djcm "1: If dis closer to (mp of house & sen) policy output than maj"


gen addir__oww_dmm=0 if __jdismed !=.
replace addir__oww_dmm=1 if __oww_dmm==0 & __jdismed !=.
replace addir__oww_dmm=1 if abs(__jmajmed-__jdismed) > abs(__jdismed-.5*(__majhmed+__majsenmed)) & __jdismed !=.

la var addir__oww_dmm "1: If dis closer to (mp of house & sen) policy output than maj"
 
 




local controls __gconflict __abct precedentalteration __ujudrev __mqsd

*construct justice FEs (maj ops):

regress __cli  __oww_dfp `controls' i.issuearea if __aumaj==1 & term <= 2012 & term >= 1947, baselev
*this gives correct e(sample)

cap drop __ajname
cap drop __ajfe
gen __ajname=justicename
levelsof justicename if e(sample), local(jjj)
foreach j of local jjj{
summ __cli if e(sample) & justicename=="`j'"
replace __ajname="OtherJustice" if `r(N)' < 30 & justicename=="`j'"
}
*to make pairwise table easier:
replace __ajname="WRehnquist" if justicename=="WHRehnquist"
replace __ajname="SBreyer" if justicename=="SGBreyer"

encode __ajname if e(sample),gen(__ajfe)

tab __ajfe

*FE for dissenting ops (going to have to go old school b/c can be 1+ author):
reg __dcli  __gconflict __abct precedentalteration __ujudrev __mqsd  i.issuea if __aumaj==1 ///
& term <= 2012 & term >= 1947 & __cli !=.
cap drop __ddALITO-__ddWHITT
gen __ddOJ=0 if e(sample)==1 
levelsof __jln, local(jjj) //if majority==1 & (opinion==2 | opinion==3)
foreach jn of local jjj{
	gen tvdd`jn'=0
	replace tvdd`jn'=1 if majority==1 & (opinion==2 | opinion==3) & __jln=="`jn'"
	egen __dd`jn'=max(tvdd`jn'),by(caseid)
	la var __dd`jn' "1: `jn' wrote dissenting op; 0: else."
	drop tvdd`jn'
	qui summ __dd`jn' if e(sample)==1, det
	if `r(sum)'<30{
		replace  __ddOJ=1 if __dd`jn'==1 
		drop __dd`jn'
	}
}


********************************************************************************
*Main Analysis alternate sample def'ns (Table 1, Main Text)
********************************************************************************
local controls __gconflict __abct precedentalteration __ujudrev __dmqsd
foreach var of varlist __oww_dfp-__oww_dmm{
local cond if __aumaj==1 & term <= 2012 & term >= 1947 & __cli !=. & addir`var'!=0  & minvotes !=0 
//given def'n of (a)ddir vars, it is important to condition on (a)ddir !=0 not (a)ddir == 1
//last condition is to remove dissents in part which are in as __dcli but not counted in Spaeth et al
summ `var' if __aumaj==1
reg __dcli `var' `cond' , cluster(term)
est store `var'adm1
reg __dcli `var' `controls' `cond' , cluster(term)
est store `var'adm2
reg __dcli `var' `controls' i.issuearea __ddOJ-__ddWARREN `cond', baselev cluster(term) 
//note that this makes White omitted FE
est store `var'adm3
}

foreach var of varlist __oww_dfp-__oww_dmm{
estout `var'adm1 `var'adm2 `var'adm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N r2)  label starlevels(* .05) keep (`var')
// starlevels(+ .1 * .05 ** .01 *** .001)
}
********************************************************************************




********************************************************************************
*interactive model, to formally compare effects across maj & diss ops.
*(Table 2, Main Text)
********************************************************************************

gen __imsamp=1 if __aumaj==1  & term <= 2012 & term >= 1947 
//we don't yet condition on "if __dcli==1" above, to allow for inclusion of 
//unanimous ops too in baseline version. will condition as part of specific reg sets l8r.
replace __imsamp=1 if __cli !=. & __aumaj==0 & term <= 2012 & term >= 1947
//this allows the following interactive model test

reg __cli __aumaj##c.__oww_dfp if __imsamp==1  //this works b/c __cli includes dissent 
//CLI for one obs st __aumaj!=1. see construction of those variables & just above.

decode __ajfe, gen(tajfen)
levelsof __jln if __aumaj==1 & tajfen !="OtherJustice" & tajfen !="", local(jjj) 
foreach jn of local jjj{
	gen __imfe`jn'=0 if __imsamp
	replace __imfe`jn'=1 if __aumaj==1 & __jln=="`jn'"
	cap replace __imfe`jn'=1 if __dd`jn'==1 & __aumaj !=1 & __cli !=.
}
gen __imfeOJ=0 if __imsamp==1
replace __imfeOJ=__ddOJ if __ddOJ !=. //b/c __ddOJ only for __aumaj==1
replace __imfeOJ=1 if tajfen=="OtherJustice" & __aumaj==1
drop tajfen


**only maj op-dissent op combos (pairs)--exlude unans & if cli missing for one of maj/diss :
local controls __gconflict __abct precedentalteration __ujudrev __cmqsd
qui foreach var of varlist __oww_dfp-__oww_dmm{
	reg __cli __aumaj##c.`var' if __imsamp==1 & addir`var'==1  ///
	& ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)) ,cluster(term) 
	//note addir !=. for aumaj==1 and ==. for unan ops; 
	//note also cluster(caseid) gives slightly smaller SEs, as expected
	est store `var'mim1a
	reg __cli __aumaj##c.`var' `controls' if __imsamp==1 & addir`var'==1 ///
	& ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)),  cluster(term)
	est store `var'mim2a
	reg __cli __aumaj##c.`var' `controls' i.issuearea __imfeALITO-__imfeOJ ///
	if __imsamp==1 & addir`var'==1 &  ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)), cluster(term)
	est store `var'mim3a
}

foreach var of varlist __oww_dfp-__oww_dmm{
estout `var'mim1a `var'mim2a `var'mim3a , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N r2)  label starlevels(* .05) keep (1.__aumaj `var'  1.__aumaj#c.`var')
// starlevels(+ .1 * .05 ** .01 *** .001)
}
********************************************************************************


********************************************************************************
*replication: (Table 3)
********************************************************************************

local controls __gconflict __abct precedentalteration __ujudrev __mqsd

foreach var of varlist __oww_dfp-__oww_dmm{
summ `var' if __aumaj==1 & term <= 2012 & term >= 1947
reg __cli `var' if __aumaj==1 & term <= 2012 & term >= 1947, cluster(term)
est store `var'm1 
reg __cli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947, cluster(term)
est store `var'm2
reg __cli `var' `controls' i.__ajfe   i.issuearea  if __aumaj==1 & term <= 2012 & term >= 1947, baselev cluster(term)
est store `var'm3
}

foreach var of varlist __oww_dfp-__oww_dmm{
estout `var'm1 `var'm2 `var'm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N bic)  label starlevels(* .05) // starlevels(+ .1 * .05 ** .01 *** .001)
}


*replication of OWW Table 1, p. 48.

estout __oww_dfpm3 __oww_dfmm3 __oww_djcmm3 __oww_dmmm3 , ///
cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N bic)  label starlevels(* .05) ///
order(__oww_dfp __oww_dfm __oww_djcm __oww_dmm ///
__gconflict __abct precedentalteration __ujudrev __mqsd) ///
keep(__oww_dfp __oww_dfm __oww_djcm __oww_dmm ///
__gconflict __abct precedentalteration __ujudrev __mqsd)


*******************************************************************************







******************Various alternative specifications****************************


********************************************************************************
*Alt spec: interactive model analogous to Table 2, keeping unan maj ops:
********************************************************************************



local controls __gconflict __abct precedentalteration __ujudrev __cmqsd
qui foreach var of varlist __oww_dfp-__oww_dmm{
	reg __cli __aumaj##c.`var' if __imsamp==1 & (addir`var'==1 | __aumaj==1),cluster(term)
	est store `var'mim1
	reg __cli __aumaj##c.`var' `controls' if __imsamp==1 & (addir`var'==1 | __aumaj==1), cluster(term)
	est store `var'mim2
	reg __cli __aumaj##c.`var' `controls' i.issuearea __imfeALITO-__imfeOJ ///
	if __imsamp==1 & (addir`var'==1 | __aumaj==1), cluster(term) 
	est store `var'mim3
}

foreach var of varlist __oww_dfp-__oww_dmm{
estout `var'mim1 `var'mim2 `var'mim3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N r2)  label starlevels(* .05) keep (1.__aumaj `var'  1.__aumaj#c.`var')
// starlevels(+ .1 * .05 ** .01 *** .001)
}

********************************************************************************









********************************************************************************
*DV: Maj CLI - Diss CLI (Appx Table [A1])
********************************************************************************

local controls __gconflict __abct precedentalteration __ujudrev __mqsd  __dmqsd
foreach var of varlist __oww_dfp-__oww_dmm{
summ `var' if __aumaj==1
reg __diff_cli `var' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1, cluster(term)
est store `var'diffm1
reg __diff_cli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947 ///
 & addir`var'==1, cluster(term)
est store `var'diffm2
reg __diff_cli `var' `controls' i.issuearea  if __aumaj==1 & term <= 2012 &  ///
term >= 1947 & addir`var'==1, cluster(term) baselev
est store `var'diffm3
}

foreach var of varlist __oww_dfp-__oww_dmm{
estout `var'diffm1 `var'diffm2 `var'diffm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(dmcrtrlN dmctrlC dmcrtrlFE) ///
legend stats(N bic)  label starlevels(* .05) keep(`var')
// starlevels(+ .1 * .05 ** .01 *** .001)
}




*****************alternative specifications (XTI, JR, J5)***********************


egen tvmm=median(XTI_justice) if majority==2, by(caseid)
egen __xjmajmed=mode(tvm), by (caseid) 
la var __xjmajmed "Median of Majority Coalition IP, XTI"
drop tvmm

egen __xjcasemed=median(XTI_justice) if majority !=., by(caseid)
la var __xjcasemed "Ct Median (Case Participants only), XTI"


gen __xoww_dfp=min(abs(__xjmajmed-__xleftfp),abs(__xjmaj-__xrightfp),abs(__xjmaj-XTI_housemed))
replace __xoww_dfp=0 if __xjmajmed > min(__xleftfp,__xrightfp,XTI_housemed) & ///
__xjmajmed < max(__xleftfp,__xrightfp,XTI_housemed) 
la var __xoww_dfp "XTI Dist to Fil Pivot (OWW method)"
 
 
gen __xoww_dfm=min(abs(__xjmajmed-XTI_senmed),abs(__xjmajmed-XTI_housemed))
replace __xoww_dfm=0 if __xjmajmed > min(XTI_housemed, XTI_senmed) & ///
__xjmajmed < max(XTI_housemed, XTI_senmed)
la var __xoww_dfm "XTI Dist to Floor Median (OWW method)"




*unconstrained if constitutional case (i.e., "judicial review")

gen __x2oww_dfp=__xoww_dfp
replace __x2oww_dfp = 0 if __jr==1
la var __x2oww_dfp "XTI Dist to Fil Pivot (OWW method, 0 if JR)"


gen __x2oww_dfm=__xoww_dfm
replace __x2oww_dfm = 0 if __jr==1
la var __x2oww_dfm "XTI Dist to Floor Median (OWW method, 0 if JR)"



*create direction var, indicating that dissent not more extreme, relative to Cong,
*in the same direction as the majority

*indicator that, for an unconstrained court, the dissent is in the same direction away from
*majority as the closest pivot.--> ~= that dissent is more compatible w/ Congress 
*than majority.  This should be true in most cases.

*these sample vars not used in this .do file, see addir vars below


egen tvxdm=median(XTI_justice) if majority==1, by(caseid)
egen __xjdismed=mode(tvxdm), by (caseid) 
la var __xjdismed "Median of Dissenting Coalition IP, XTI"
drop tvxdm

*we violate naming convention here to allow use of the var variants in a loop
gen ddir__xoww_dfp=0 if __xjdismed !=.
replace ddir__xoww_dfp=1 if __xoww_dfp==0 & __xjdismed !=.
replace ddir__xoww_dfp=1 if __xjmajmed < min(__xleftfp,__xrightfp,XTI_housemed) ///
& __xjdismed > __xjmajmed & __xjdismed !=.
replace ddir__xoww_dfp=1 if __xjmajmed > max(__xleftfp,__xrightfp,XTI_housemed) ///
& __xjdismed < __xjmajmed & __xjdismed !=.

la var ddir__xoww_dfp "1: If maj const & dis is in dir of cong (FP), rel to maj; or maj unconst (XTI)"
*this var is to define a sample


gen ddir__xoww_dfm=0 if __xjdismed !=.
replace ddir__xoww_dfm=1 if __xoww_dfm==0 & __xjdismed !=.
replace ddir__xoww_dfm=1 if __xjmajmed < min(XTI_senmed,XTI_housemed) & __xjdismed > __xjmajmed & __xjdismed !=.
replace ddir__xoww_dfm=1 if __xjmajmed > max(XTI_senmed,XTI_housemed) & __xjdismed < __xjmajmed & __xjdismed !=.
 
la var ddir__xoww_dfm "1: If maj const & dis is in dir of cong (Floor Med), rel to maj; or maj unconst (XTI)"



*sample vars:

gen ddir__x2oww_dfp=ddir__xoww_dfp
replace ddir__x2oww_dfp=1 if __x2oww_dfp==0 & ddir__xoww_dfp !=.
//b/c if for x2oww_dfp certain obs = 0 that !=0 for xoww_dfp--only diff b/w vars. 


gen ddir__x2oww_dfm=ddir__xoww_dfm
replace ddir__x2oww_dfm=1 if __x2oww_dfm==0 & ddir__xoww_dfm !=.
//see above





*more rigorous sample var def'n--does dissent prefer cong policy (midpoint of
*house and senate medians) to majority opinion.

*we violate naming convention here to allow use of the var variants in a loop
gen addir__xoww_dfp=0 if __xjdismed !=.
replace addir__xoww_dfp=1 if __xoww_dfp==0 & __xjdismed !=.
replace addir__xoww_dfp=1 if abs(__xjmajmed-__xjdismed) > abs(__xjdismed-.5*(XTI_housemed+XTI_senmed)) ///
& __jdismed !=.


la var addir__xoww_dfp "1: If dis closer to (mp of house & sen) policy output than maj"


gen addir__xoww_dfm=0 if __xjdismed !=.
replace addir__xoww_dfm=1 if __xoww_dfm==0 & __xjdismed !=.
replace addir__xoww_dfm=1 if abs(__xjmajmed-__xjdismed) > abs(__xjdismed-.5*(XTI_housemed+XTI_senmed)) ///
& __jdismed !=.
//really could just do gen addir_xoww_dfm=addir_xoww_dfp.

la var addir__xoww_dfm "1: If dis closer to (mp of house & sen) policy output than maj"





*sample vars:

gen addir__x2oww_dfp=addir__xoww_dfp
replace addir__x2oww_dfp=1 if __x2oww_dfp==0 & addir__xoww_dfp !=.
//b/c if for x2oww_dfp certain obs = 0 that !=0 for xoww_dfp--only diff b/w vars. 


gen addir__x2oww_dfm=addir__xoww_dfm
replace addir__x2oww_dfm=1 if __x2oww_dfm==0 & addir__xoww_dfm !=.
//see above





local controls __gconflict __abct precedentalteration __ujudrev __mqsd
foreach var of varlist __xoww_dfp-__x2oww_dfm{
summ `var' if __aumaj==1
reg __cli `var' if __aumaj==1 & term <= 2012 & term >= 1947
est store `var'm1 
reg __cli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947
est store `var'm2
reg __cli `var' `controls' i.issuearea i.__ajfe if __aumaj==1 & term <= 2012 & term >= 1947, baselev
est store `var'm3
}

local controls __gconflict __abct precedentalteration __ujudrev __dmqsd
foreach var of varlist __xoww_dfp-__x2oww_dfm{
summ `var' if __aumaj==1 & term <= 2012 & term >= 1947
reg __dcli `var' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'dm1
reg __dcli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'dm2
reg __dcli `var' `controls' i.issuearea __ddOJ-__ddWARREN if __aumaj==1 & ///
term <= 2012 & term >= 1947 & addir`var'==1, baselev
est store `var'dm3
}

local controls __gconflict __abct precedentalteration __ujudrev __mqsd __dmqsd
foreach var of varlist __xoww_dfp-__x2oww_dfm{
summ `var' if __aumaj==1 & term <= 2012 & term >= 1947
reg __diff_cli `var' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'diffm1
reg __diff_cli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'diffm2
reg __diff_cli `var' `controls' i.issuearea  if __aumaj==1 & ///
term <= 2012 & term >= 1947 & addir`var'==1, baselev
est store `var'diffm3
}



foreach var of varlist __xoww_dfp-__x2oww_dfm{
estout `var'm1 `var'm2 `var'm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N bic)  label starlevels(* .05)  keep (`var') // starlevels(+ .1 * .05 ** .01 *** .001)
}

foreach var of varlist __xoww_dfp-__x2oww_dfm{
estout `var'dm1 `var'dm2 `var'dm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(dissN dissC dissFE) ///
legend stats(N bic)  label starlevels(* .05) keep (`var') // starlevels(+ .1 * .05 ** .01 *** .001)
} 

foreach var of varlist __xoww_dfp-__x2oww_dfm{
estout `var'diffm1 `var'diffm2 `var'diffm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(diffN diffC diffFE) ///
 legend stats(N bic)  label starlevels(* .05) keep (`var') // starlevels(+ .1 * .05 ** .01 *** .001)
}



********************************************************************************
*Interactive analysis (XTI IPs, OWW and no-JR definitions of constraint) (Appx Table [A2])
********************************************************************************

**only maj op-dissent op combos (pairs)--exlude unans & if cli missing for one of maj/diss :
local controls __gconflict __abct precedentalteration __ujudrev __cmqsd
qui foreach var of varlist __xoww_dfp-__x2oww_dfm{
	reg __cli __aumaj##c.`var' if __imsamp==1 & addir`var'==1  ///
	& ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)) ,cluster(term) 
	//note addir !=. for aumaj==1 and ==. for unan ops
	est store `var'mim1a
	reg __cli __aumaj##c.`var' `controls' if __imsamp==1 & addir`var'==1 ///
	& ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)),  cluster(term)
	est store `var'mim2a
	reg __cli __aumaj##c.`var' `controls' i.issuearea __imfeALITO-__imfeOJ ///
	if __imsamp==1 & addir`var'==1 &  ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)), cluster(term)
	est store `var'mim3a
}

foreach var of varlist __xoww_dfp-__x2oww_dfm{
estout `var'mim1a `var'mim2a `var'mim3a , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N r2)  label starlevels(* .05) keep (1.__aumaj `var'  1.__aumaj#c.`var')
// starlevels(+ .1 * .05 ** .01 *** .001)
}
********************************************************************************




*last: check with Ct median as maj IP

gen __xj5_dfp=min(abs(__xjcasemed-__xleftfp),abs(__xjcasemed-__xrightfp),abs(__xjcasemed-XTI_housemed))
replace __xj5_dfp=0 if __xjcasemed > min(__xleftfp,__xrightfp,XTI_housemed) & ///
__xjcasemed < max(__xleftfp,__xrightfp,XTI_housemed) 
la var __xj5_dfp "XTI Dist to Fil Pivot (Ct IP=J5)"
 
 
gen __xj5_dfm=min(abs(__xjcasemed-XTI_senmed),abs(__xjcasemed-XTI_housemed))
replace __xj5_dfm=0 if __xjcasemed > min(XTI_housemed, XTI_senmed) & ///
__xjcasemed < max(XTI_housemed, XTI_senmed)
la var __xj5_dfm "XTI Dist to Floor Median (Ct IP=J5)"


*unconstrained if constitutional case (i.e., "judicial review")

gen __x2j5_dfp=__xj5_dfp
replace __x2j5_dfp = 0 if __jr==1
la var __x2j5_dfp "XTI Dist to Fil Pivot (Ct IP=J5, 0 if JR)"


gen __x2j5_dfm=__xj5_dfm
replace __x2j5_dfm = 0 if __jr==1
la var __x2j5_dfm "XTI Dist to Floor Median (Ct IP=J5)"




*we violate naming convention here to allow use of the var variants in a loop

gen addir__xj5_dfp=0 if __xjdismed !=.
replace addir__xj5_dfp=1 if __xj5_dfp==0 & __xjdismed !=.
replace addir__xj5_dfp=1 if abs(__xjcasemed-__xjdismed) > abs(__xjdismed-.5*(XTI_housemed+XTI_senmed)) ///
& __jdismed !=.


la var addir__xj5_dfp "1: If dis closer to (mp of house & sen) policy output than maj (XTI, J5)"
*this var is to define a sample


gen addir__xj5_dfm=addir__xj5_dfp
la var addir__xj5_dfm "1: If dis closer to (mp of house & sen) policy output than maj (XTI, J5)"



*sample vars:

gen addir__x2j5_dfp=addir__xj5_dfp
replace addir__x2j5_dfp=1 if __x2j5_dfp==0 & addir__xj5_dfp !=.
//b/c if for x2j5_dfp certain obs = 0 that !=0 for xj5_dfp--only diff b/w vars. 


gen addir__x2j5_dfm=addir__xj5_dfm
replace addir__x2j5_dfm=1 if __x2j5_dfm==0 & addir__xj5_dfm !=.
//see above



local controls __gconflict __abct precedentalteration __ujudrev __mqsd
foreach var of varlist __xj5_dfp-__x2j5_dfm{
summ `var' if __aumaj==1
reg __cli `var' if __aumaj==1 & term <= 2012 & term >= 1947
est store `var'm1 
reg __cli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947
est store `var'm2
reg __cli `var' `controls' i.issuearea i.__ajfe if __aumaj==1 & term <= 2012 & term >= 1947, baselev
est store `var'm3
}

local controls __gconflict __abct precedentalteration __ujudrev __dmqsd
foreach var of varlist __xj5_dfp-__x2j5_dfm{
summ `var' if __aumaj==1 & term <= 2012 & term >= 1947
reg __dcli `var' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'dm1
reg __dcli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'dm2
reg __dcli `var' `controls' i.issuearea __ddOJ-__ddWARREN if __aumaj==1 & ///
term <= 2012 & term >= 1947 & addir`var'==1, baselev
est store `var'dm3
}

local controls __gconflict __abct precedentalteration __ujudrev __mqsd __dmqsd
foreach var of varlist __xj5_dfp-__x2j5_dfm{
summ `var' if __aumaj==1 & term <= 2012 & term >= 1947
reg __diff_cli `var' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'diffm1
reg __diff_cli `var' `controls' if __aumaj==1 & term <= 2012 & term >= 1947 & addir`var'==1
est store `var'diffm2
reg __diff_cli `var' `controls' i.issuearea  if __aumaj==1 & ///
term <= 2012 & term >= 1947 & addir`var'==1, baselev
est store `var'diffm3
}



foreach var of varlist __xj5_dfp-__x2j5_dfm{
estout `var'm1 `var'm2 `var'm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N bic)  label starlevels(* .05)  keep (`var') // starlevels(+ .1 * .05 ** .01 *** .001)
}

foreach var of varlist __xj5_dfp-__x2j5_dfm{
estout `var'dm1 `var'dm2 `var'dm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(dissN dissC dissFE) ///
legend stats(N bic)  label starlevels(* .05) keep (`var') // starlevels(+ .1 * .05 ** .01 *** .001)
} 

foreach var of varlist __xj5_dfp-__x2j5_dfm{
estout `var'diffm1 `var'diffm2 `var'diffm3 , cells(b(star fmt(3)) se(par fmt(2))) mlabels(diffN diffC diffFE) ///
 legend stats(N bic)  label starlevels(* .05) keep (`var') // starlevels(+ .1 * .05 ** .01 *** .001)
}



********************************************************************************
*Interactive analysis, XTI IPs, Ct Median (J5) as Ct IP, OWW and no-JR def'ns of constraint
*Appx Table [A3]
********************************************************************************


**only maj op-dissent op combos (pairs)--exlude unans & if cli missing for one of maj/diss :
local controls __gconflict __abct precedentalteration __ujudrev __cmqsd
qui foreach var of varlist __xj5_dfp-__x2j5_dfm{
	reg __cli __aumaj##c.`var' if __imsamp==1 & addir`var'==1  ///
	& ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)) ,cluster(term) 
	//note addir !=. for aumaj==1 and ==. for unan ops
	est store `var'mim1a
	reg __cli __aumaj##c.`var' `controls' if __imsamp==1 & addir`var'==1 ///
	& ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)),  cluster(term)
	est store `var'mim2a
	reg __cli __aumaj##c.`var' `controls' i.issuearea __imfeALITO-__imfeOJ ///
	if __imsamp==1 & addir`var'==1 &  ((__dcli !=. & __aumaj==1) | (__aumaj==0 & __mcli!=.)), cluster(term)
	est store `var'mim3a
}

foreach var of varlist __xj5_dfp-__x2j5_dfm{
estout `var'mim1a `var'mim2a `var'mim3a , cells(b(star fmt(3)) se(par fmt(2))) mlabels(repN repC repFE) ///
legend stats(N r2)  label starlevels(* .05) keep (1.__aumaj `var'  1.__aumaj#c.`var')
// starlevels(+ .1 * .05 ** .01 *** .001)
}
********************************************************************************



***************show that ns of OWW replication not due to OWW sample************



local controls __gconflict __abct precedentalteration __ujudrev __mqsd
reg __cli __oww_dfp `controls' i.__ajfe   i.issuearea  if __aumaj==1 & term <= 2012 ///
& term >= 1947, baselev cluster(term)
gen tn=_n
gen tsamp=e(sample)
gen sampcoeff=.
set seed 81921 //date
qui forvalues i=1/500{
	gen rv=runiform()
	gsort - tsamp  rv
	local controls __gconflict __abct precedentalteration __ujudrev __mqsd
	reg __cli __oww_dfp `controls' i.__ajfe   i.issuearea  if __aumaj==1 & term <= 2012 ///
	& term >= 1947 & _n<=529, baselev cluster(term) //(conditions besides _n <= 529 redundant)
	replace sampcoeff =  _b[__oww_dfp] if tn==`i'
	drop rv
}

summ sampc, det
*always << 1.81
drop tsamp tn // sampcoeff 

********************************************************************************
